<?php
/**
 * @Description: 学分培训
 * @Author: kzj
 * @datetime: 2023-02-24
 */
namespace App\Services\Faculty;

use App\Http\Controllers\Excel\ExcelExport;
use App\Models\Faculty\PlatformCredit;
use App\Models\System\Dicitem;
use App\Models\Teachers;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;

/**
 * 平台学分 (服务)
 */
class PlatformCreditService
{

    /**
     * @Description: 获取列表信息数据
     * @Author: kzj
     * @datetime: 2022-11-21
     */
    public function getList($request)
    {
        $page = $request['page'] ?? 1;
        $limit = $request['limit'] ?? 10;
        // dd($request, 1);

        $filters = [];
        if (!empty($request['search']['course_name'])){
            $filters[] = ['course_name', 'like', '%'.$request['search']['course_name'].'%'];
        }
        if (!empty($request['search']['course_level'])){
            $filters[] = ['course_level', $request['search']['course_level']];
        }

        $user = Auth::user();

        $result = PlatformCredit::where('user_id', '>', 0)
            ->with('user:id,nickname')
            ->with('teacher:sx_id,name')
            ->when(!empty($request['search']['user_name']), function($q) use($request) {
                $q->whereHas('teacher', function($q) use($request) {
                    $q->where('name', 'like', '%'.$request['search']['user_name'].'%');
                });
            })
            ->when(!empty($filters), function($q) use($filters) {
                $q->where($filters);
            })
            ->when(!$user->is_checker, function($q) use($user){
                $q->where('user_id', $user->teacher?$user->teacher->id:-1);
            })
            ->when(isset($request['user_id']) && !empty($request['user_id']), function($q) use($request){
                $teacher = Teachers::where('user_id', $request['user_id'])->first();
                if(!empty($teacher)) {
                    $q->whereRaw("user_id = " . $teacher->id);
                }
            })
            ->paginate($limit, '*', 'page', $page)
            ->toArray();
        $result['data'] = collect($result['data'])->map(function($item) {
            $item['teacher_name'] = $item['teacher']['name'];
            return $item;
        });
        return $result;
    }

    /**
     * @Description: 获取平台学分列表信息数据
     * @Author: kzj
     * @datetime: 2023-02-21
     */
    public function getUserList($request)
    {
        $page = $request['page'] ?? 1;
        $limit = $request['limit'] ?? 10;
        // dd($request, 1);

        $filters = [];
        if (!empty($request['search']['user_name'])){
            $filters[] = ['user_name', 'like', '%'.$request['search']['user_name'].'%'];
        }

        $userId = $request['search']['user_id'];

        $result = PlatformCredit::where('user_id', $userId)
            ->with('user:id,nickname')
            ->when(!empty($filters), function($q) use($filters) {
                $q->where($filters);
            })
            ->paginate($limit, '*', 'page', $page)
            ->toArray();

        return $result;
    }


    /**
     * @Description: 学分培训数据导出
     * @Author: kzj
     * @datetime: 2023-02-28
     */
    public function export($request)
    {
        // dd($request);
        $filters = [];

        $filters = [];
        if (!empty($request['search']['user_name'])){
            $filters[] = ['user_name', 'like', '%'.$request['search']['user_name'].'%'];
        }

        $user = Auth::user();

        $output = PlatformCredit::with('user:id,nickname')
            ->when(!empty($filters), function($q) use($filters) {
                $q->where($filters);
            })
            ->when(!$user->is_checker, function($q) use($user){
                $q->where('user_id', $user->id);
            })
            ->get();

        $title = ['时期','姓名','课程名称','课程级别','获得学分','获得时间'];
        $exportData = [];
        foreach($output as $k => $v){
            $item = [
                'term' => $v->term ? $v->term : '',
                'name' => $v->user_name,
                'course_name' => $v->course_name ? $v->course_name : '',
                'course_level' => $v->course_level ? $v->course_level : '',
                'score' => $v->score,
                'get_time' => $v->get_time,
            ];

            $exportData[] = $item;
        }
        array_unshift($exportData, $title);

        $fileName = md5(auth()->user()->id.time()).'.xlsx';
        Excel::store(new ExcelExport($exportData), $fileName, 'excel');

        $result['file_name'] = $fileName;

        return $result;
    }
}
